package com.opennews.openplatform.familyexpenseapi.config

import com.opennews.openplatform.familyexpenseapi.security.SecurityActiveTokenManager
import com.opennews.openplatform.familyexpenseapi.service.*
import com.opennews.openplatform.myspringbootcore.security.RequestMapManager
import org.springframework.boot.CommandLineRunner
import org.springframework.stereotype.Component

@Component
class DataInitializer(
    private val appReleaseLogService: AppReleaseLogService,
    private val bankService: BankService,
    private val incomeExpenseDetailBaseTypeService: IncomeExpenseDetailBaseTypeService,
    private val regionService: RegionService,
    private val requestMapService: RequestMapService,
    private val requestMapManager: RequestMapManager,
    private val roleService: RoleService,
    private val securityActiveTokenManager: SecurityActiveTokenManager,
) : CommandLineRunner {
    override fun run(vararg args: String?) {
        // For Initializing data if needed.
        appReleaseLogService.init()
        bankService.init()
        incomeExpenseDetailBaseTypeService.init()
        regionService.init()
        requestMapService.init()
        roleService.init()

        // To make the setting work we have to clear the cache.
        // Just in case any error causes requestMap failed. Clears the cache once system startup.
        // For example, the Requestmap.httpMethod value must be uppercase. Lowercase value would cause clearCachedRequestmaps failed.
        // Once clearCachedRequestmaps failed, any access will be blocked.
        // BE CAUTION!!!!
        //
        // IMPORTANT!!!
        // If you see the error of "No enum constant org.springframework.http.HttpMethod." when you are trying to run the project, go and
        // check the Requestmap.httpMethod values in the database table.
        // This field only accepts the string value of HTTP methods. Any empty string or other string values will cause this error.
        requestMapManager.reloadCachedRequestMaps()

        securityActiveTokenManager.reloadCachedUserActiveTokenRecords()
    }
}